using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using emp.Model;
using emp.BLL;
using emp.IDAL;
using emp.BLL.SysManage;
using emp.IDAL.SysManage;

namespace emp.ProcessFlow
{
    public class ItemController
    {
        private const string URL_DEFAULT = "item_list.aspx";
        private const string URL_ITEMCREATE = "item_add.aspx";
        private const string URL_ITEMUPDATE = "item_modify.aspx";

        /// <summary>
        /// Default constructor
        /// </summary>
        public ItemController()
        {
        }

        public bool CreateItem(ItemInfo newItemInfo)
        {

            try
            {
                // Creata a new business logic tier
                Item item = new Item();

                // Call the insert method
                item.Insert(newItemInfo);

                //Finally forward to the welcome page
                HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// A method to process an updated account
        /// </summary>
        /// <param name="updatedAccountInfo">Updated account information</param>
        public void UpdateItem(ItemInfo updatedItemInfo)
        {

            // Create the business logic tier
            Item item = new Item();

            // Call the udpate method
            item.Update(updatedItemInfo);

            //Redirect the user to the my account page
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);

        }

        /// <summary>
        /// A method to process an updated account
        /// </summary>
        /// <param name="updatedAccountInfo">Updated account information</param>
        public void DeleteItem(int iNodeID)
        {

            // Create the business logic tier
            Item item = new Item();

            // Call the udpate method
            item.Delete(iNodeID);

            //Redirect the user to the my account page
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);

        }


        public string ConvertItem(int Item_ID)
        {

            // Create the business logic tier
            Item item = new Item();

            // Call the udpate method
            return item.ConvertItem(Item_ID);

         

        }

        public int getItemIDByDesc(string item_value, int parent_item_id)
        {
            Item item = new Item();

            // Call the udpate method
            return item.getItemIDByDesc(item_value, parent_item_id);
        }

        public bool CreateTreeView(TreeView tv, string addUrl, string uptUrl, string delUrl)
        {

            try
            {
                Tree tree = new Tree();
                TreeNode tn = tree.CreateRoot(tv, addUrl, uptUrl, delUrl);
                ITree sMenu = DALFactory.Item.Create();
                tree.TreeViewBind(tn, 0, addUrl, uptUrl, delUrl,sMenu,"0");
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool CreateTreeView(TreeView tv, int pID)
        {

            try
            {
                Tree tree = new Tree();
                ITree sMenu = DALFactory.Item.Create();
                TreeNode tn = tree.CreateRoot(tv, pID, sMenu);

                tree.TreeViewBind(tn, pID,sMenu);
            }
            catch
            {
                return false;
            }

            return true;
        }


        public bool CreateRadioTreeView(TreeView tv, int pID)
        {

            try
            {
                Tree tree = new Tree();
                ITree sMenu = DALFactory.Item.Create();
                TreeNode tn = tree.CreateRoot(tv, pID, sMenu);

                tree.TreeViewRadioBind(tn, pID, sMenu);
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}
